07. Quiz: Find the Corners

Now, it's your turn! Given this image of waffles on a checked table cloth, find the corners on this image. Notice as you test your code, which corners are the strongest, and pick a threshold that allows most of the corners (especially in the focused foreground) to be detected, as shown below. Notice what points qualify as corners!

Waffles with corners detected

Waffles with corners detected

Start Quiz:

import matplotlib.pyplot as plt
import numpy as np
import cv2

# Read in the image
image = cv2.imread('waffles.jpg')
# Convert it to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# ---------------------------------------------------------- #


## TODO: Complete this corner detection function
## This takes in an image
## And returns dilated corners
def corner_detect(image):
    # Convert the image to grayscale, floating point values
    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    gray = np.float32(gray)
    
    ## TODO: Create a Harris corner detector using those grayscale vals
    ## Change this value, but keep the variable name
    corners = None
    
    ## TODO: Dilate the corner detections
    
    # Return those values
    return corners

# Runs your function (do not change this line of code)
corners = corner_detect(image)

## TODO: Define a threshold to select strong corners
threshold = 0

# ---------------------------------------------------------- #
if(corners is not None):
    # Create an image copy to draw corners on
    corner_image = np.copy(image)

    # Iterate through all the corners and draw them on the image (if they pass the threshold)
    for j in range(0, corners.shape[0]):
        for i in range(0, corners.shape[1]):
            if(corners[j,i] > threshold):
                # image, center pt, radius, color, thickness
                cv2.circle(corner_image, (i, j), 1, (0,255,0), 1)      
                
    # Plot the result
    f, (ax1, ax2) = plt.subplots(1, 2, figsize=(20, 10))
    f.tight_layout()
    ax1.imshow(corners, cmap='gray')
    ax1.set_title('Dilated Corners')
    ax2.imshow(corner_image, cmap='gray')
    ax2.set_title('Thresholded Corners')